import Dialog from "@/js/Dialog.js";
import CustomQuery from "@/views/modules/form/customForm/js/CustomQuery";
import {cloneDeep} from 'lodash-es'
import FormBase from "../js/FormBase";
import baseButtonFunc from "@/js/baseButtonFunc.js"
import MFormTitle from '@/views/modules/MForm/MFormItem/MFormTitle.vue'
import RxButtonList from "@/components/RxButtonList"
import Util from "@/js/Util.js";
export default {
    mixins:[FormBase,baseButtonFunc],
    components: {
        MFormTitle,
        RxButtonList,
    },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"componentsLang",key);
        },
        initButtonData(){
          let deleteList = ['fullScreen',"excelImport","excelExport"]
          this.buttonData = [];
          this.metadata.buttonConfig.buttonData.forEach((res) => {
              if (!deleteList.includes(res.method) &&   this.buttonPermission[res.key]) {
                  this.buttonData.push(res)
              }
          })
        },
        /*
            按钮方法
         */
        handMethod(btn) {
            let self_ = this;
            if (btn.type == "custom") {
                let formBuild = this.formVm
                if (formBuild && formBuild[btn.method]) {
                    formBuild[btn.method](this, btn);
                } else {
                    uni.showToast({
                        title: `${btn.method},`+this.i18n('NotDefined','还没有定义!'), duration: 2000, icon: 'none'
                    });
                }
                return;
            } else if (btn.type == "default") {
                this[btn.method]()
            } else if (btn.type == "customDialog") {
                let dialogConfig = btn.btnConfig;
                let gridInput = dialogConfig.gridInput;
                //构建查询参数
                let params = CustomQuery.getParamsInput(gridInput, this.formVm.data, this.curVal);
                let conf = {
                    data: {
                        dialogOptions: {
                            title: JSON.parse(btn.btnConfig.dialogAlias).label,
                            okText:this.i18n('confirm','确定')
                        },
                        alias: JSON.parse(btn.btnConfig.dialogAlias).value,
                        single: btn.btnConfig.singleType=='single'?true:false,
                        params: Object.keys(params).map(function (key) {
                            return key + "=" + params[key];
                        }).join("&"),
                    },
                    curVm: this,
                };
                let eventConfig = btn.btnConfig
                if(eventConfig.beforeMethod){
                    if(!this.formVm[eventConfig.beforeMethod]){
                        this.$message.warning(this.i18n('beforeMethod','请检查js脚本中是否有设置前置方法!'))
                        return;
                    }
                    //增加前置方法.
                    let isBefore = this.formVm[eventConfig.beforeMethod](this.data);
                    if(isBefore && isBefore.hasOwnProperty('success')){
                        if(!isBefore.success){
                            if(isBefore.msg){
                                this.$message.warning(isBefore.msg)
                            }else {
                                this.$message.warning(this.i18n('noBeforeMethod','未满足打开对话框所需的前置条件~'))
                            }
                            return;
                        }
                    }
                }
                Dialog.openDialog(conf, function (data) {
                    if (data.action == "ok") {
                        let rows = data.data.rows;
                        if (!rows) {
                            rows = data;
                        }
                        if (Array.isArray(rows)) {
                            for (let i = 0; i < rows.length; i++) {
                                let initRow = cloneDeep(self_.formVm.data.initData[dialogConfig.gridName.replace('sub__','')]) ;
                                //绑定映射行
                                let newData={...initRow};
                                //绑定映射行
                                newData.index_ = Util.randomId();
                                self_.bindRows(dialogConfig, [rows[i]], false, newData,'',function () {
                                    let canAdd = self_.canAddRow(dialogConfig.uniqueField, newData, self_.curVal);
                                    if (canAdd) {
                                        self_.curVal.push(newData);
                                    }
                                });
                            }
                        } else {
                            //绑定映射行
                            let newData = {};
                            rows = [rows];
                            newData.index_ = Util.randomId();
                            //绑定映射行
                            self_.bindRows(dialogConfig, rows, false, newData,function (){
                                let canAdd = self_.canAddRow(dialogConfig.uniqueField, newData, self_.curVal);
                                if (canAdd) {
                                    self_.curVal.push(newData);
                                }
                            });
                        }
                        if (dialogConfig.callback) {
                            self_.formVm[dialogConfig.callback](self_.curVal);
                        }
                    }
                })
            }
        },
    },
    computed: {
        /**
         * 从菜单获取按钮权限.
         * @returns {*|{}}
         */
        buttonPermission() {
            let buttonsInit= {
                    add:true,
                    delete:true,
                    downMove:true,
                    fullScreen:true,
                    upMove:true,
            }
            let buttons = this.localPermission.subTable[this.subKey] && this.localPermission.subTable[this.subKey].buttons || buttonsInit;
            return buttons;
        },
        hasShowOrIf() {
            return (item) => {
                if (!item.control.conditionConfig) {
                    return;
                }
                let {conditionConfig: {hideMethod}} = item.control;
                return hideMethod;
            }
        },

    },
    watch: {
        value: {
            handler(v) {
                this.curVal = v;
            },
            deep: true,
            immediate: true
        },
    }
}
